package com.huxiu.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import com.huxiu.APP;
import com.huxiu.AuthenticatorActivity;
import com.huxiu.R;
import com.huxiu.utils.Constants;
import com.huxiu.utils.Utils;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * Created by tian on 14-2-19.
 */
public class WXEntryActivity extends AuthenticatorActivity implements IWXAPIEventHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IWXAPI mWeixinShareApi = WXAPIFactory.createWXAPI(this, Constants.WECHAT_KEY, false);
        mWeixinShareApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp){
        int result;
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (baseResp instanceof SendAuth.Resp) {
                    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(APP.getInstance());
                    Intent intent = new Intent(Constants.ACTION_WECHAT_LOGIN);
                    intent.putExtra("result", baseResp.errCode == BaseResp.ErrCode.ERR_OK);
                    //                    Log.d("result", " " + (baseResp.errCode == BaseResp.ErrCode.ERR_OK));
                    //                    Log.d("code", ((SendAuth.Resp) baseResp).code);
                    intent.putExtra("code", ((SendAuth.Resp) baseResp).code);
                    lbm.sendBroadcast(intent);
                    finish();
                }
                if(Constants.WX_TYPE.equals(Constants.WX_SHARE)){
                    Utils.showToast("分享成功");
                }else if(Constants.WX_TYPE.equals(Constants.WX_LOGIN)){
                    Utils.showToast("登录成功");
                }
                Intent intent = new Intent(Constants.INTENT_USER_BIND_OK);
                //                    Gson gson = new Gson();
                //                    String json = gson.toJson(response.data);
                //                    intent.putExtra("loginJson", json);
                LocalBroadcastManager.getInstance(APP.getInstance()).sendBroadcast(intent);
                result = R.string.errcode_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }
        //        Utils.showToast(result);//这里如果微信登录出错时可以启用
        finish();
    }

}
